<html>
<head><meta charset="utf-8"><title>moving transmute&#x27;s size check to where bounds · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html">moving transmute&#x27;s size check to where bounds</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244203493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244203493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244203493">(Jun 28 2021 at 20:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> <a href="#narrow/stream/131828-t-compiler/topic/reifying.20function.20ptrs.20for.20intrinsics.20.2384297/near/244199327">said</a>:</p>
<blockquote>
<p><code>transmute</code> has to be, because its size check can't be expressed with a <code>where</code> bound (<em>yet</em>)</p>
</blockquote>
<p>mwahahaha</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics)]</span><span class="w"></span>
<span class="cp">#![feature(const_evaluatable_checked)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span><span class="p">;</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Satisfied</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Predicate</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">B</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Satisfied</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Predicate</span><span class="o">&lt;</span><span class="kc">true</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">transmogrify</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="n">e</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">U</span><span class="w"></span>
<span class="k">where</span><span class="w"> </span><span class="n">Predicate</span><span class="o">&lt;</span><span class="p">{</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">()}</span><span class="o">&gt;</span>: <span class="nc">Satisfied</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">transmogrify</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// compiles</span>
<span class="w">    </span><span class="c1">// let z: u8 = unsafe { transmogrify(x) }; // compiler error</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="244203569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244203569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244203569">(Jun 28 2021 at 20:57)</a>:</h4>
<p>sadly it's weirder than that, but it <em>could</em> be an intrinsic or a builtin trait</p>



<a name="244203676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244203676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244203676">(Jun 28 2021 at 20:58)</a>:</h4>
<p>(it can handle situations where <code>size_of</code> can't give you a value, like <code>Box&lt;T&gt;</code> vs <code>*mut T</code> with <code>T: ?Sized</code>)</p>



<a name="244203743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244203743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244203743">(Jun 28 2021 at 20:59)</a>:</h4>
<p>(yes, actually attempting to replace <code>todo!</code> with <code>transmute</code> gives me some guff about "dependently-sized types" :P )</p>



<a name="244203943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244203943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244203943">(Jun 28 2021 at 21:00)</a>:</h4>
<p>oh that's just the compiler still doing its own checks, which can't really ever work with your where bound</p>



<a name="244204067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244204067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244204067">(Jun 28 2021 at 21:01)</a>:</h4>
<p>what I mean is a deeper mismatch tho</p>



<a name="244204291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244204291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244204291">(Jun 28 2021 at 21:03)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> so, say, if the current behavior was just "the two sizes must be equal", then your approach would be fine, and we'd just need to remove the builtin check. the <code>where</code> clause could even go on the intrinsic definition itself. but that's sadly <em>not</em> equivalent</p>



<a name="244204402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244204402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244204402">(Jun 28 2021 at 21:04)</a>:</h4>
<p>the current behavior is more like "there must be a valid reason to be able to assume <code>T</code> and <code>U</code> <em>will</em> have the same size, across every monomorphization of the caller"</p>



<a name="244204457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244204457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244204457">(Jun 28 2021 at 21:05)</a>:</h4>
<p>the sizes being constant (like in your <code>where</code> clause) is just one of supported "solutions"</p>



<a name="244204602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244204602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244204602">(Jun 28 2021 at 21:06)</a>:</h4>
<p>I alluded to the other one (at least I think there's only two main ones) - which btw I think even works with e.g. <code>Option&lt;Box&lt;T&gt;&gt;</code> or <code>Option&lt;Rc&lt;T&gt;&gt;</code></p>



<a name="244210023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244210023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244210023">(Jun 28 2021 at 22:01)</a>:</h4>
<p>There was even a suggestion for adding such a special rule for <code>U = MaybeUninit&lt;T&gt;</code>, IIRC</p>



<a name="244217678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244217678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Wagner-Hall <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244217678">(Jun 28 2021 at 23:35)</a>:</h4>
<p>While we're on the topic, can I cheekily invite any reviews of <a href="https://github.com/rust-lang/rust/pull/86281">https://github.com/rust-lang/rust/pull/86281</a> ? :)</p>



<a name="244318111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/moving%20transmute%27s%20size%20check%20to%20where%20bounds/near/244318111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/moving.20transmute&#x27;s.20size.20check.20to.20where.20bounds.html#244318111">(Jun 29 2021 at 17:33)</a>:</h4>
<p>That was the suggestion I had in mind <span aria-label="big smile" class="emoji emoji-1f604" role="img" title="big smile">:big_smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>